namespace Maple.Branch.Settings.DomainTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using EfCoreConfigurations;
    using Entities;
    using TestBases;

    public abstract class DomainTestBase : TestBase<SettingsDomainTestModule>
    {
        protected virtual void UsingDbContext(Action<ISettingsDbContext> action)
        {
            using var dbContext = GetRequiredService<ISettingsDbContext>();

            action.Invoke(dbContext);
        }

        protected virtual T UsingDbContext<T>(Func<ISettingsDbContext, T> action)
        {
            using var dbContext = GetRequiredService<ISettingsDbContext>();

            return action.Invoke(dbContext);
        }

        protected List<Setting> GetSettingsFromDbContext(string entityType, string entityId, string name)
        {
            return UsingDbContext(context =>
                context.Settings.Where(
                    s =>
                        s.ProviderName == entityType &&
                        s.ProviderKey == entityId &&
                        s.Name == name
                ).ToList()
            );
        }
    }
}
